Array.prototype.contains = function (obj) {
    var i = this.length;
    while (i--) { if (this[i] == obj) return true; }
    return false;
}

function parseJSONDateTime(dateTime) {
    return new Date(parseInt(dateTime.substr(6)));
}

function coalesce(object1, object2) {
    if (object1)
        return object1;

    return object2;
}

function getDistance(lat1, lon1, lat2, lon2) {
    var unit = 'M'; //miles
    var radlat1 = Math.PI * lat1 / 180
    var radlat2 = Math.PI * lat2 / 180
    var radlon1 = Math.PI * lon1 / 180
    var radlon2 = Math.PI * lon2 / 180
    var theta = lon1 - lon2
    var radtheta = Math.PI * theta / 180
    var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
    dist = Math.acos(dist)
    dist = dist * 180 / Math.PI
    dist = dist * 60 * 1.1515
    if (unit == "K") { dist = dist * 1.609344 }
    if (unit == "N") { dist = dist * 0.8684 }
    return dist
}         

function makeRequest(requestUrl, callback) {
    var connector;

    if (requestUrl.match("/?"))
        connector = "&";
    else
        connector = "?";
    $.getJSON(requestUrl + connector + "api_key=M8xudPElsU2Gwe87MzYKVg&$format=json&$callback=?", callback);
}


var date_sort_asc = function (date1, date2) {
    // This is a comparison function that will result in dates being sorted in
    // ASCENDING order. As you can see, JavaScript's native comparison operators
    // can be used to compare dates. This was news to me.
    if (date1 > date2) return 1;
    if (date1 < date2) return -1;
    return 0;
};

var date_sort_desc = function (date1, date2) {
    // This is a comparison function that will result in dates being sorted in
    // DESCENDING order.
    if (date1 > date2) return -1;
    if (date1 < date2) return 1;
    return 0;
}

Array.prototype.where = function (predicate) {
    var result = new Array();

    $.each(this, function () {
        if (predicate(this))
            result.push(this);
    });
    return result;
}

Array.prototype.take = function (count) {
    if (this.length > 0)
        return this.slice(0, count);

    return new Array();
}

function getGpsLocation() {
    return { Lat: 38.905157, Lng: -77.066329 }
}